package ae.pcfc.cldos.online.report.service.provider;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.SoapMessage;

import ae.pcfc.cldos.online.fnp.ConstantsFNP;
import ae.pcfc.cldos.online.fnp.service.client.BaseFNP;
import ae.pcfc.cldos.online.report.ConstantsReport;
import ae.pcfc.cldos.online.report.service.GSReportDetailsService;
import ae.pcfc.cldos.online.report.service.types.ArrayOfEmployeeMasterReport;
import ae.pcfc.cldos.online.report.service.types.ArrayOfTransactionMasterReport;
import ae.pcfc.cldos.online.report.service.types.EmployeeMasterReport;
import ae.pcfc.cldos.online.report.service.types.GetEmployeeMasterReport;
import ae.pcfc.cldos.online.report.service.types.GetEmployeeMasterReportResponse;
import ae.pcfc.cldos.online.report.service.types.GetTransactionMasterReport;
import ae.pcfc.cldos.online.report.service.types.GetTransactionMasterReportResponse;
import ae.pcfc.cldos.online.report.service.types.TransactionMasterReport;
import ae.pcfc.cldos.online.report.service.vo.EmployeeMasterReportReqVO;
import ae.pcfc.cldos.online.report.service.vo.EmployeeMasterReportResponseVO;
import ae.pcfc.cldos.online.report.service.vo.EmployeeTransactionMasterReportResponseVO;
import ae.pcfc.cldos.online.report.service.vo.EmployeeTransactionReportReqVO;
import ae.pcfc.cldos.online.ws.base.BaseWebService;

/**
 * 
 * @author akhader
 *
 */
//public class GSReportDetailsServiceImpl extends BaseWebService implements GSReportDetailsService {
public class GSReportDetailsServiceImpl implements GSReportDetailsService {
	
	protected WebServiceTemplate wsTemplateReport;
	protected String formsAppURLReport;

	@Value("${report.url}")
	String classURIValue = ConstantsReport.REPORT_WS_ENDPOINT;
	


	/**
	 * @param formsAppURL the formsAppURL to set
	 */
	@Resource(name = "formsAppURLReport")
	public void setFormsAppURLReport(String formsAppURLReport) {
		this.formsAppURLReport = formsAppURLReport;
	}
	/**
	 * @param wsTemplateFNP
	 *            the wsTemplateFNP to set
	 */
	@Resource(name = "wsTemplateReport")
	public void setWsTemplateReport(WebServiceTemplate wsTemplateReport) {
		this.wsTemplateReport = wsTemplateReport;
	}

	
	private static DatatypeFactory df = null;     
	static {         
		try {             
			df = DatatypeFactory.newInstance();         
		} catch (DatatypeConfigurationException dce) {             
			throw new IllegalStateException(                 
					"Exception while obtaining DatatypeFactory instance", dce);         
		}     
	}        
	/*** Converts a java.util.Date into an instance of XMLGregorianCalendar      *      * 
	@param date Instance of java.util.Date or a null reference      
	* @return XMLGregorianCalendar instance whose value is based upon the      
	*  value in the date parameter. If the date parameter is null then      *  this method will simply return null.      */    
	public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
		if (date == null) {
			return null;
		} else {
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTimeInMillis(date.getTime());
			return df.newXMLGregorianCalendar(gc);
		}
	} 


	/**
	 * @param employeeMasterReportReqVO
	 * @return
	 */
	public List<EmployeeMasterReportResponseVO> getEmployeeMasterReportData(EmployeeMasterReportReqVO employeeMasterReportReqVO){
		List<EmployeeMasterReportResponseVO> empMasterList = new ArrayList<EmployeeMasterReportResponseVO>();
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		GetEmployeeMasterReport getEmployeeMasterReport = new GetEmployeeMasterReport();
		getEmployeeMasterReport.setCompanyLicenseNo(employeeMasterReportReqVO .getCompanyLicenseNo());
		getEmployeeMasterReport.setEmployeeStatus(employeeMasterReportReqVO.getEmployeeStatus());
		if(employeeMasterReportReqVO.getEpExpiryDateFrom() != null){
			getEmployeeMasterReport.setEntryPermitExpiryDateFrom(formatter.format(employeeMasterReportReqVO.getEpExpiryDateFrom()));
		}
		if(employeeMasterReportReqVO.getEpExpiryDateTo() != null){
			getEmployeeMasterReport.setEntryPermitExpiryDateTo(formatter.format(employeeMasterReportReqVO.getEpExpiryDateTo()));
		}
		getEmployeeMasterReport.setEmployeeName(employeeMasterReportReqVO.getName());
		getEmployeeMasterReport.setNationality(employeeMasterReportReqVO.getNationality());
		getEmployeeMasterReport.setPassportNumber(employeeMasterReportReqVO.getPassportNumber());
		if(employeeMasterReportReqVO.getRpExpiryDateFrom() != null){
			getEmployeeMasterReport.setResidencePermitExpiryDateFrom(formatter.format(employeeMasterReportReqVO.getRpExpiryDateFrom()));
		}
		if(employeeMasterReportReqVO.getRpExpiryDateTo() != null){
			getEmployeeMasterReport.setResidencePermitExpiryDateTo(formatter.format(employeeMasterReportReqVO.getRpExpiryDateTo()));
		}
		
		GetEmployeeMasterReportResponse response = (GetEmployeeMasterReportResponse) wsTemplateReport
				.marshalSendAndReceive(classURIValue,getEmployeeMasterReport,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsReport.GET_EMP_MASTER_REPORT);
							}
						});
		
		ArrayOfEmployeeMasterReport empList = response.getGetEmployeeMasterReportResult();
		if(empList.getEmployeeMasterReport()!=null){
		for(EmployeeMasterReport employeeMasterReport : empList.getEmployeeMasterReport()){
				EmployeeMasterReportResponseVO employeeMasterReportResponseVO = new EmployeeMasterReportResponseVO();
				employeeMasterReportResponseVO.setCecNo(employeeMasterReport.getCECNumber());
				employeeMasterReportResponseVO.setEmployeeStatus(employeeMasterReport.getEmployeeStatus());
				employeeMasterReportResponseVO.setEntryPermitExpiryDate(employeeMasterReport.getEntryPermitExpiryDate());
				employeeMasterReportResponseVO.setEntryPermitNumber(employeeMasterReport.getEntryPermitNumber());
				employeeMasterReportResponseVO.setName(employeeMasterReport.getEmployeeName());
				employeeMasterReportResponseVO.setNationality(employeeMasterReport.getNationality());
				employeeMasterReportResponseVO.setPassportNumber(employeeMasterReport.getPassportNumber());
				employeeMasterReportResponseVO.setPositionHeld(employeeMasterReport.getPositionHeld());
				employeeMasterReportResponseVO.setResidencePermitExpiryDate(employeeMasterReport.getResidencePermitExpiryDate());
				employeeMasterReportResponseVO.setResidencePermitNumber(employeeMasterReport.getResidencePermitNumber());
				employeeMasterReportResponseVO.setWfStatus(employeeMasterReport.getStatus());
				empMasterList.add(employeeMasterReportResponseVO);
			}
		}
		return empMasterList;
		
	}
	
	/**
	 * @param employeeTransactionReportReqVO
	 * @return
	 */
	public List<EmployeeTransactionMasterReportResponseVO> getEmployeeTransactionReportData(EmployeeTransactionReportReqVO employeeTransactionReportReqVO){
		List<EmployeeTransactionMasterReportResponseVO> transMasterList = new ArrayList<EmployeeTransactionMasterReportResponseVO>();
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		GetTransactionMasterReport getTransactionMasterReport = new GetTransactionMasterReport();
		getTransactionMasterReport.setCompanyLicenseNo(employeeTransactionReportReqVO .getCompanyLicenseNo());
		getTransactionMasterReport.setCandidateName(employeeTransactionReportReqVO.getCandidateName());
		getTransactionMasterReport.setProcessCode(employeeTransactionReportReqVO.getProcessCode());
		if(employeeTransactionReportReqVO.getTransactionDateFrom() != null){
			getTransactionMasterReport.setTransactionDateFrom(formatter.format(employeeTransactionReportReqVO.getTransactionDateFrom()));
		}
		if(employeeTransactionReportReqVO.getTransactionDateTo() != null){
			getTransactionMasterReport.setTransactionDateTo(formatter.format(employeeTransactionReportReqVO.getTransactionDateTo()));
		}
		getTransactionMasterReport.setTransactionNumber(employeeTransactionReportReqVO.getTransactionNumber());
		getTransactionMasterReport.setTransactionStatus(employeeTransactionReportReqVO.getTransactionStatus());
		
		GetTransactionMasterReportResponse response = (GetTransactionMasterReportResponse) wsTemplateReport
				.marshalSendAndReceive(classURIValue,getTransactionMasterReport,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsReport.GET_EMP_TRANSACTION_REPORT);
							}
						});
		
		ArrayOfTransactionMasterReport empList = response.getGetTransactionMasterReportResult();
		if(empList.getTransactionMasterReport()!=null){
		for(TransactionMasterReport transactionMasterReport : empList.getTransactionMasterReport()){
				EmployeeTransactionMasterReportResponseVO tranReportResponseVO = new EmployeeTransactionMasterReportResponseVO();
				tranReportResponseVO.setCandidateName(transactionMasterReport.getCandidateName());
				tranReportResponseVO.setCecNumber(transactionMasterReport.getCECNumber());
				tranReportResponseVO.setIncidentNumber(transactionMasterReport.getIncidentNumber());
				tranReportResponseVO.setProcessName(transactionMasterReport.getProcessName());
				tranReportResponseVO.setTransactionDate(transactionMasterReport.getTransactionDate());
				tranReportResponseVO.setTransactionNumber(transactionMasterReport.getTransactionNumber());
				tranReportResponseVO.setTransactionStatus(transactionMasterReport.getTransactionStatus());
				tranReportResponseVO.setTransactionStatusDesc(transactionMasterReport.getTransactionStatusDesc());
				tranReportResponseVO.setAdviseId(transactionMasterReport.getAdviseId());
				transMasterList.add(tranReportResponseVO);
			}
		}
		return transMasterList;
		
	}
	
}
